import RadioButton from './RadioButton';
const {ccclass, property} = cc._decorator;


type rb = RadioButton;


@ccclass
export default class CreateRoom extends cc.Component{


    /**血流成河跟血战到底的按钮集合 */
    leixingxuanze: rb[] = [];
    /**血流成河各种选项跟血战到底各种选项的容器节点 */
    gameList: cc.Node = null;

    /**血战到底或血流成河的包围节点 */
    currentGame: cc.Node = null;

    lastType:string;

    onLoad(){
        this.gameList = this.node.getChildByName('game_list');
        this.leixingxuanze = this.getRadioButtonArray(this.node, 'leixingxuanze');
    }


    getRadioButtonArray(node:cc.Node, name: string){
        const t = node.getChildByName(name);
        let arr: rb[] = [];
        for (let i = 0; i < t.children.length; i++) {
            let n = t.children[i].getComponent(RadioButton);
            if (n) {
                arr.push(n);
            }
        }
        return arr;
    }

    /**
     * 关闭选项界面
     */
    onBtnBack(){
        this.node.active = false;
    }


    /**
     * 确定按钮点击
     */
    onBtnOK(){
        const usedTypes = ['xzdd', 'xlch'];
        const type = this.getType();
        if(usedTypes.indexOf(type) == -1){
            return;
        }
        this.node.active = false;
        this.createRoom();
    }

    /**
     * 整理好用户的选择发送创建房间请求
     */
    createRoom(){
        const onCreate = (ret: cc.ret) => {
            if(ret.errcode !== 0){
                cc.vv.wc.hide();
                if(ret.errcode == 2222){
                    cc.vv.alert.show('提示', '钻石不足创建房间失败');
                }
                else{
                    cc.vv.alert.show('提示', '创建房间失败，错误码:' + ret.errcode);
                }
            }
            else{
                cc.vv.gameNetMgr.connectGameServer(ret);
            }
        };

        const type = this.getType();
        let conf = this.constructSCMJConf();
        conf.type = type;

        const data: cc.ret = {
            account: cc.vv.userMgr.account,
            sign: cc.vv.userMgr.sign,
            conf: JSON.stringify(conf),
        };
        cc.vv.alert.show('正在创建房间', '');
        cc.vv.http.sendRequest('/create_private_room', data, onCreate);
    }


    /**
     * 获取整理玩家选择的选项信息
     */
    constructSCMJConf(){
        const wanFaXuanZe  = this.currentGame.getChildByName('wanfaxuanze');
        const huanSanZhang = wanFaXuanZe.children[0].getComponent('CheckBox').checked;
        const jiangDui     = wanFaXuanZe.children[1].getComponent('CheckBox').checked;
        const menQing      = wanFaXuanZe.children[2].getComponent('CheckBox').checked;
        const tianDiHu     = wanFaXuanZe.children[3].getComponent('CheckBox').checked;

        const diFen       = this.getSelectedOfRadioGroup('difenxuanze');
        const ziMo        = this.getSelectedOfRadioGroup('zimojiacheng');
        const zuiDaFanShu = this.getSelectedOfRadioGroup('zuidafanshu');
        const juShuXuanZe = this.getSelectedOfRadioGroup('xuanzejushu');
        const dianGangHua = this.getSelectedOfRadioGroup('dianganghua');

        return <cc.conf>{
            diFen,
            ziMo,
            jiangDui,
            huanSanZhang,
            zuiDaFanShu,
            juShuXuanZe,
            dianGangHua,
            menQing,
            tianDiHu,
        };
    }


    getSelectedOfRadioGroup(groupRoot: string){

        const arr = this.getRadioButtonArray(this.currentGame, groupRoot);

        let selected = 0;
        for(let i = 0; i < arr.length; i++){
            if(arr[i].checked){
                selected = i;
                break;
            }
        }
        return selected;
    }

    getType(){
        let type = 0;
        for(let i = 0; i < this.leixingxuanze.length; i++){
            if(this.leixingxuanze[i].checked){
                type = i;
                break;
            }
        }
        return type == 0 ? 'xzdd' : type == 1 ? 'xlch' : 'xzdd';
    }


    

    update(dt){
        const type = this.getType();
        if(this.lastType != type){
            this.lastType = type;
            this.gameList.children.map((node) => {
                node.active = false;
            });
            const game = this.gameList.getChildByName(type);
            if(game){
                game.active = true;
            }
            this.currentGame = game;
        }
    }
}